package com.ruoyi.generator.service;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.generator.domain.GenTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author luowu
 * @Date 2024/8/9 涵 11:28:57
 */
@Service
public class GenCodeServiceImpl implements GenCodeService{

    @Resource
    private IGenTableService genTableService;

    @Resource
    private IGenTableColumnService genTableColumnService;

    @Override
    public byte[] downloadCode(String tableName) {
        // 同步自定义表和表字段到genTable和genTableColumn表
        insertGenTable(tableName);
        // 2.同步自定义表结构到genTable表
        // genTableService.synchDb(tableName);
        // 生成代码
        return genTableService.downloadCode(tableName);
    }

    private void insertGenTable(String tableName){
        String[] tableNames = Convert.toStrArray(tableName);
        // 查询表信息
        List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
        genTableService.importGenTable(tableList);
    }
}
